package cn.wit.testalgorithms;

import java.lang.reflect.Array;
import java.util.Arrays;

import org.junit.Test;

/**
 * 交换相邻字符
 * 
 * @author 16604
 *
 *         RXXLRXRXL XRLXXRRLX
 */

public class DemoSwapAdjacentChar {
	public static void main(String[] args) {
		String start = "RXXLRXRXL";
		String end = "XRLXXRRLX";
		// System.out.println(start.charAt(0));
		System.out.println(canTransform(start, end));

	}

	@Test
	public static boolean canTransform(String start, String end) {

		// 因为String字符串不可修改，所以这里转换成StringBulider
		StringBuilder sb = new StringBuilder(start);
		int flag = 0;

		// 遍历整个数组
		for (int i = 0; i < sb.length() - 1; i = i + 2) {
			// 判断有没有可以替换得字符
			if (sb.charAt(i) == 'X' && sb.charAt(i + 1) == 'L') {
				sb.setCharAt(i, 'L');
				sb.setCharAt(i + 1, 'X');
				// System.out.println(i);

			} else if (sb.charAt(i) == 'R' && sb.charAt(i + 1) == 'X') {
				sb.setCharAt(i, 'X');
				sb.setCharAt(i + 1, 'R');
				// System.out.println(i);  
			}
		}
		System.out.println(sb);

		if (sb.equals(end))
			return true;
		else
			return false;
	}
}